package com.sugon.controller.device;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.dao.DevMaintenanceTicketItemDao;
import com.sugon.entity.DevMaintenanceTicketItemEntity;
import com.sugon.modules.device.model.dto.ModifyLiveImageParam;
import com.sugon.modules.device.service.IDeviceMaintenanceManager;
import com.sugon.service.DevMaintenanceTicketItemService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import com.sugon.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 设备维护工单项Controller
 *
 * @author YuXD
 * @date 2020-11-18 11:44
 */
@Controller
@RequestMapping("devplangc")
public class DevMaintenanceTicketItemController {

    @Autowired
    private DevMaintenanceTicketItemDao devMaintenanceTicketItemDao;
    @Autowired
    private DevMaintenanceTicketItemService devMaintenanceTicketItemService;
    @Autowired
    private IDeviceMaintenanceManager deviceMaintenanceManager;

    /**
     * 查询临时工单项
     */
    @RequestMapping("/temTicketItems")
    @ResponseBody
    public R temTicketItems(@RequestParam Map<String, Object> params) {
        // 校验工单ID必填
        String ticketId = MapUtil.getStr(params, "ticketId");
        if (StrUtil.isEmpty(ticketId)) {
            return R.error("维护工单必选！");
        }
        //查询列表数据
        Query query = new Query(params);
        List<Map<String, Object>> temTicketItems = devMaintenanceTicketItemDao.listTemTicketItems(ticketId);
        PageUtils pageUtil = new PageUtils(temTicketItems, temTicketItems.size(), query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查询计划工单项列表
     */
    @RequestMapping("/planTicketItems")
    @ResponseBody
    public R planTicketItems(@RequestParam Map<String, Object> params) {
        // 校验工单ID必填
        String ticketId = MapUtil.getStr(params, "ticketId");
        if (StrUtil.isEmpty(ticketId)) {
            return R.error("维护工单必选！");
        }
        //查询列表数据
        Query query = new Query(params);
        List<Map<String, Object>> temTicketItems = devMaintenanceTicketItemDao.listPlanTicketItems(ticketId);
        PageUtils pageUtil = new PageUtils(temTicketItems, temTicketItems.size(), query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    @AutoLog("批量结束设备维护工单项")
    @RequestMapping("/temTicketItems/batchEnd")
    @ResponseBody
    public R batchEnd(@RequestBody Map<String, String[]> params) {
        String[] ticketItemIds = params.get("ids");
        if (ArrayUtil.isNotEmpty(ticketItemIds)) {
            for (String ticketItemId : ticketItemIds) {
                // 校验设备维护项是否存在
                DevMaintenanceTicketItemEntity devMaintenanceTicketItem = devMaintenanceTicketItemService.queryObject(ticketItemId);
                if (devMaintenanceTicketItem == null) {
                    return R.error("设备维护项不存在!");
                }
                // 校验是否已完成
                if (devMaintenanceTicketItem.getStatus() == 1) {
                    return R.error("当前维护项目已完成!");
                }
                devMaintenanceTicketItem.setImplementerId(ShiroUtils.getUserId());
                deviceMaintenanceManager.completeDevMaintenanceOption(devMaintenanceTicketItem);
            }
        }
        return R.ok();
    }

    @RequiresPermissions("devplangc:modifyLiveImage")
    @NonBusinessParamValid
    @AutoLog("修正现场照片")
    @RequestMapping("/temTicketItems/modifyLiveImage")
    @ResponseBody
    public R modifyLiveImage(ModifyLiveImageParam modifyLiveImageParam, BindingResult bindingResult) {
        // 校验设备维护项是否存在
        DevMaintenanceTicketItemEntity devPmTicketItem = devMaintenanceTicketItemService.queryObject(modifyLiveImageParam.getPmTicketItemId());
        if (devPmTicketItem == null) {
            return R.error("设备维护项不存在!");
        }
        return deviceMaintenanceManager.modifyLiveImage(devPmTicketItem, modifyLiveImageParam.getLiveImages()) ? R.ok() : R.error("修正失败，请稍后重试！");
    }

}
